/**
 * Copyright 2014 the original author or authors. All rights reserved.
 */
package com.visionet.security.domain.dto;

import java.util.ArrayList;
import java.util.List;

import com.visionet.jumper.core.support.spring.DomainTransfer;
import com.visionet.security.base.constants.Constants;
import org.apache.commons.lang3.StringUtils;

import com.visionet.security.domain.entity.Resource;
import com.visionet.security.base.enums.ResourceType;

/**
 *  url 资源信息
 *
 * @author suxiaojing
 * @date 
 * @since 0.0.1
 */
public class UrlizedResource extends DomainTransfer<Resource> {
	
	private String id;
	
	private String name;
	
	private ResourceType type;
	
	private String typeDesc;
	
	private String parentId;

	private String definition;
	
	private String url;
	
	private String domain;
	
	private Integer level;
	
	private Integer orderNo;
	
	private String permission;
	
	private List<UrlizedResource> subResource = new ArrayList<>();


	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the type
	 */
	public ResourceType getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(ResourceType type) {
		this.type = type;
	}

	/**
	 * @return the parentId
	 */
	public String getParentId() {
		return parentId;
	}

	/**
	 * @param parentId
	 *            the parentId to set
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the domain
	 */
	public String getDomain() {
		return domain;
	}

	/**
	 * @param domain
	 *            the domain to set
	 */
	public void setDomain(String domain) {
		this.domain = domain;
	}

	/**
	 * @return the level
	 */
	public Integer getLevel() {
		return level;
	}

	/**
	 * @param level
	 *            the level to set
	 */
	public void setLevel(Integer level) {
		this.level = level;
	}

	/**
	 * @return the orderNo
	 */
	public Integer getOrderNo() {
		return orderNo;
	}

	/**
	 * @param orderNo
	 *            the orderNo to set
	 */
	public void setOrderNo(Integer orderNo) {
		this.orderNo = orderNo;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the permission
	 */
	public String getPermission() {
		return permission;
	}

	/**
	 * @param permission
	 *            the permission to set
	 */
	public void setPermission(String permission) {
		this.permission = permission;
	}
	

	/**
	 * @return the typeDesc
	 */
	public String getTypeDesc() {
		return typeDesc;
	}

	/**
	 * @param typeDesc the typeDesc to set
	 */
	public void setTypeDesc(String typeDesc) {
		this.typeDesc = typeDesc;
		this.type=ResourceType.valueOf(typeDesc);
	}

	/**
	 * @return the subResource
	 */
	public List<UrlizedResource> getSubResource() {
		return subResource;
	}

	/**
	 * @param subResource the subResource to set
	 */
	public void setSubResource(List<UrlizedResource> subResource) {
		this.subResource = subResource;
	}

	public String getDefinition() {
		return definition;
	}

	public void setDefinition(String definition) {
		this.definition = definition;
	}

	/**
	 * Generate pattern for menu resource
	 * 
	 * @param permissionInherit
	 * @return The final pattern for menu resource
	 */
	private String generateMenuPattern(boolean permissionInherit) {
		String pattern = StringUtils.substringBefore(url, "?");
		if (permissionInherit) {
			return StringUtils.substringBeforeLast(pattern, "/") + "/**";
		}
		return pattern;
	}

	public String getPattern(boolean permissionInherit) {
		switch (type) {
		case BUTTON:
			break;
		case PATH:
			return generatePathPattern(permissionInherit);
		case MENU:
			return generateMenuPattern(permissionInherit);
		case METHOD:
			break;
		default:
			break;
		}
		return null;
	}

	/**
	 * Generate pattern for path resource
	 * 
	 * @param permissionInherit
	 * @return The final pattern for path resource
	 */
	private String generatePathPattern(boolean permissionInherit) {
		if(StringUtils.contains(url, "*")){
			return url;
		}
		if(StringUtils.contains(url, "?") && permissionInherit){
			return StringUtils.substringBefore(url, "?")+"?*";
		}
		return url;
	}
	
	/**
	 * @return
	 */
	public String generatePermissionString(){
		StringBuilder builder=new StringBuilder(domain);
		builder.append(Constants.PERMISSION_SEPARATOR);
		builder.append(permission);
		return builder.toString();
	}

}
